import 'package:flutter/material.dart';

import 'package:wanandroid/pages/article/article_page.dart';
import 'package:wanandroid/pages/home/home_page.dart';
import 'package:wanandroid/pages/knowledge/knowledge_page.dart';
import 'package:wanandroid/pages/my_drawer/my_drawer.dart';
import 'package:wanandroid/pages/navi/navi_page.dart';
import 'package:wanandroid/pages/project/project_page.dart';
import 'package:wanandroid/pages/search/search_page.dart';

class App extends StatefulWidget {
  const App({ super.key });

  @override
  AppState createState() => AppState();
}

//应用页面状态实现类
class AppState extends State<App> {
  int _selectedIndex = 0; //当前选中项的索引

  final appBarTitles = ['玩Android', '体系', '文章', '导航', "项目"];

  var pages = <Widget>[
     HomePage(),
     KnowledgePage(),
     ArticlePage(),
     NaviPage(),
     ProjectPage()
  ];

  @override
  Widget build(BuildContext context) {
     Color themeColor = Theme.of(context).primaryColor;
      return Scaffold(
        drawer: const MyDrawer(),
        appBar: AppBar(
          title: Text(appBarTitles[_selectedIndex], style: const TextStyle(color: Colors.white),),
          bottom: null,
          backgroundColor: themeColor,
          iconTheme: const IconThemeData(color: Colors.white),
          elevation: 0,
          actions: <Widget>[
            IconButton(
                icon: const Icon(Icons.search),
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                    return const SearchPage();
                  }));
                })
          ],
        ),
        body: IndexedStack(index: _selectedIndex, children: pages),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
            BottomNavigationBarItem(icon: Icon(Icons.assignment), label: '体系'),
            BottomNavigationBarItem(icon: Icon(Icons.chat), label: '文章'),
            BottomNavigationBarItem(icon: Icon(Icons.navigation), label: '导航'),
            BottomNavigationBarItem(icon: Icon(Icons.book), label: '项目')
          ],
          type: BottomNavigationBarType.fixed, //设置显示的模式
          currentIndex: _selectedIndex,
          onTap: _onItemTapped,
        ),
      );
  }

  //选择按下处理 设置当前索引为index值
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  Future<bool> _showBackDialog() async {
    final shouldPop = await showDialog<bool>(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('提示'),
        content: const Text('确定退出应用吗？'),
        actions: <Widget>[
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text('再看一会'),
          ),
          TextButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: const Text('退出'),
          ),
        ],
      ),
    );
    return shouldPop ?? false;
  }
}
